from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models

# AbstractUser 抽象的父类
from itsdangerous.jws import TimedJSONWebSignatureSerializer


class User(AbstractUser):
    """用户模型类"""
    # 补充手机号字段
    # mobile = models.CharField(max_length=11, unique=True, verbose_name='手机号')
    mobile = models.CharField(max_length=11, verbose_name='手机号')
    # 新增一个字段: 邮箱激活状态
    email_active = models.BooleanField(default=False, verbose_name='邮箱验证状态')

    class Meta:
        db_table = 'tb_users'
        verbose_name = 'QQ登陆用户数据'
        verbose_name_plural = verbose_name

    def generate_active_email(self):
        """生成激活连接"""
        """
        http://www.meiduo.site:8080/success_verify_email.html?user_id=1&email=xxx@163.com
        http://www.meiduo.site:8080/success_verify_email.html?token=xxx
        """
        # 创建itsdagerous的对象
        s = TimedJSONWebSignatureSerializer(settings.SECRET_KEY,60*60*24) # 有效期１天
        # 操作对象对token进行加密
        token = s.dumps({'user_id':self.id,'email':self.email}).decode()
        # 生成激活连接加token发到邮箱
        active_url = 'http://www.meiduo.site:8080/success_verify_email.html?token='+token
        return active_url
